var app = getApp();
import {
  api,
  util,
  dialog,
} from '../../../../js/util';
import {
  router
} from '../../../../js/router';
import {
  group
} from '../../../../js/modules/market';
import {
  $wuxCountDown,
  $ecCountDown
} from '../../../../components/index'
import {
  list
} from "../../../../js/modules/mall";
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"panicBuyList",
  /**
   * 页面的初始数据
   */
  data: {
    PageIndex: 1,
    PageSize: 20,
    Keyword:"",
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    sortField: "Reorder",
    sortDirect: "desc",
    hasMoreData: true,
    dataType: 1, //状态 1正在进行 0即将开始
    goodsList: [],
    timer:null,
    sortList: [
      { name: '默认', sortcolligate: 'i-down', dataname: 'Reorder' },
      { name: '销量', sortcolligate: '', dataname: 'BuyQuantity' },
      { name: '价格', sortcolligate: '', dataname: 'Price' }
    ],
    hasFilter: false,
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      PageIndex: 1,
      Currency:api.Currency || '￥'
    });
    this.getWholesaleConfig();
    this.loadPanicProductList(()=>{
      let { dataType}=this.data;
      if (dataType == 1){
        dataType = 2;
        this.setData({
          dataType: dataType
        },()=>{
          this.loadPanicProductList();
        });
      }

    });
  },

  /**分页加载限时特价商品列表 */
  loadPanicProductList(callback) {
    var that = this;
    var param = {
      PageIndex: that.data.PageIndex,
      PageSize: that.data.PageSize,
      keywords: that.data.Keyword,
      sortField: that.data.sortField,
      sortDirect: that.data.sortDirect,
      dataType: that.data.dataType
    };
    clearInterval(this.data.timer);
    this.data.timer = null;
    group.loadPanicProductList(param, function(res) {
      var contentlistTem = that.data.goodsList;
      if (that.data.PageIndex == 1) {
        contentlistTem = [];
      }
      var contentlist = res.Content.data;
      var goodlist = contentlistTem.concat(contentlist);
      that.setData({
        goodsList: goodlist,
        hasMoreData: res.Content.count > (that.data.PageIndex) * that.data.PageSize
      }); 
      if(res.Content.count ==0){
        if (callback && typeof (callback) == "function") {
          callback();
        }
      }else{
        group.setCountDown(that);
      }
    });
  },
  /**加载商品类的配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    list.getWholesaleConfigs(
      fkid,
      fkflag,
      function (content) {
        that.setData({
          quantityDigits: content.QuantityDigits,
          priceDigits: content.PriceDigits,
        });
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    if (this.data.timer) {
      clearInterval(this.data.timer)
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    if (this.data.timer) {
      clearInterval(this.data.timer)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.data.PageIndex =1;
    this.loadPanicProductList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.hasMoreData) {
      this.data.PageIndex = this.data.PageIndex + 1;
      this.loadPanicProductList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onSearchEvent(e){
    var keyword=e.detail;
    this.setData({
      Keyword: keyword,
      PageIndex:1
    });
    this.loadPanicProductList();
  },
  onSortEvent(e){
    var sortField = e.detail.sortField;
    var sortDirect = e.detail.sortDirect;
    this.data.PageIndex = 1;
    this.setData({
      sortField,
      sortDirect,
      PageIndex: this.data.PageIndex
    })
    this.loadPanicProductList();
  },
  /**
   * tab切换
   */
  changeCurrent: function(e) {
    var activeIndex = e.currentTarget.dataset.index;
    this.setData({
      dataType: activeIndex,
      PageIndex: 1
    },function(){
      this.loadPanicProductList();
    });
  },
  toDetail(e) {
    var idx = e.currentTarget.dataset.idx;
    var item = this.data.goodsList[idx];
    var ptype=3;
   
    if (item.PanicStatus == -1 || this.data.dataType==0) {
      ptype=0;
    }
    if (item.IsStart != 1){
      ptype = 0;
    }
    router.navigate({ 
      path: "productDetail",
      params:{
        ptype: ptype,
        id: item.ProductId,
        sid: item.FKId,
        discounttype:2,
        discountid: item.Id
      }
     });

  }
})